package com.sskj.givecoin.v.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.p.fragment.LoginFragmentPresenter;
import com.sskj.givecoin.utils.CheckUtil;
import com.sskj.givecoin.utils.ClickUtil;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.activity.GoogleLoginActivity;
import com.sskj.givecoin.v.activity.LoginActivity;
import com.sskj.givecoin.v.activity.MainActivity;
import com.sskj.givecoin.v.activity.PwdForgetActivity;
import com.sskj.givecoin.v.base.BaseFragment;

import butterknife.BindView;

public class LoginFragment extends BaseFragment<LoginActivity, LoginFragmentPresenter> {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.login_pwd)
    TextView loginPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_toggle_pwd)
    ImageView ivTogglePwd;
    @BindView(R.id.pwd_show)
    RelativeLayout pwdShow;
    @BindView(R.id.yzm_login)
    TextView yzmLogin;
    @BindView(R.id.register_input_auth_code)
    EditText registerInputAuthCode;
    @BindView(R.id.register_get_auth_code)
    TextView registerGetAuthCode;
    @BindView(R.id.yzm_show)
    RelativeLayout yzmShow;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.tv_pwd_forget)
    TextView tvPwdForget;
    @BindView(R.id.ivClear)
    ImageView ivClear;
    private boolean isSee;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    public LoginFragmentPresenter getPresenter() {
        return new LoginFragmentPresenter();
    }

    public static LoginFragment newInstance() {

        Bundle args = new Bundle();

        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        ClickUtil.click(btLogin, () -> {
            if (CheckUtil.isMobile(etPhone.getText().toString()) || CheckUtil.isEmail(etPhone.getText().toString())) {  // 邮箱 手机号校验
                if (etPwd.getText().length() >= 6 && etPwd.getText().length() <= 20) {
                    mPresenter.login(etPhone.getText().toString(), etPwd.getText().toString());
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strLoginActivity200));
                }

            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strLoginActivity201));
            }
        });
        ClickUtil.click(400, ivTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                etPwd.setTransformationMethod(new PasswordTransformationMethod());
                ivTogglePwd.setImageResource(R.mipmap.unsee);
            } else {
                isSee = true;
                etPwd.setTransformationMethod(new HideReturnsTransformationMethod());
                ivTogglePwd.setImageResource(R.mipmap.see);
            }
        });
        ClickUtil.click(ivClear, () -> {
            etPhone.setText("");
        });
        ClickUtil.click(tvPwdForget, () -> {
            startActivity(new Intent(getActivity(), PwdForgetActivity.class));
        });
                etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
//                if (CheckUtil.isMobile(s.toString()) || CheckUtil.isEmail(s.toString())) {
//                    btLogin.setEnabled(true);
//                } else {
//                    btLogin.setEnabled(false);
//                }
            }
        });
    }

    /**
     * 登录成功
     */
    public void loginSuccess(int type) {  // 0 关闭  1 开启

        SPUtils.put(MyAppLication.getApplication(), Constants.SP_LOGIN_STATUS, true);
        startActivity(new Intent(getActivity(), MainActivity.class));
        getActivity().finish();


    }
}
